package com.tecode.controller;

import com.tecode.bean.Favorite;
import com.tecode.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/10.
 */
@Controller
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;

    /**
     * 获取收藏分类列表
     * @param userId
     * @return
     */
    @GetMapping("/getFavoriteType")
    @ResponseBody
    public List getFavoriteType(@RequestParam Integer userId){
        List types = favoriteService.getTypeListByUserId(userId);
        return types;
    }

    /**
     * 获取指定收藏分类中的歌曲列表
     * @param favoriteId
     * @return
     */
    @GetMapping("/getFavorites/{favoriteId}")
    @ResponseBody
    public List getFavorites(@PathVariable("favoriteId") Integer favoriteId){
        List favoritesById = favoriteService.getFavoritesById(favoriteId);
        System.out.println(favoritesById);
        return favoritesById;
    }

    /**
     * 跳转编辑页面的数据回显
     * @param favoriteId
     * @return
     */
    @GetMapping("edit")
    @ResponseBody
    public Favorite edit(@RequestParam Integer favoriteId){
        Favorite favorite = favoriteService.getFavoriteById(favoriteId);
        System.out.println(favorite);
        return favorite;
    }

    /**
     * 保存修改后的数据
     * @param favorite
     * @return
     */
    @PutMapping("save")
    @ResponseBody
    public boolean save(@RequestBody Favorite favorite) {
        int save = favoriteService.save(favorite);
        return save > 0 ? true : false;
    }

    /**
     * 删除收藏分类
     * @param favoriteId
     * @return
     */
    @DeleteMapping("del/{favoriteId}")
    @ResponseBody
    public boolean del(@PathVariable("favoriteId") Integer favoriteId){
        int del = favoriteService.del(favoriteId);
        return del > 0 ? true : false;
    }
}
